//
//  MyAssetsVC.m
//  AppNew
//
//  Created by 华伟 on 2019/4/4.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "MyAssetsVC.h"
#import "AccountCell.h"
#import "AccountModel.h"
@interface MyAssetsVC ()<UITableViewDelegate, UITableViewDataSource>{
    NSInteger _oldIndex;
}

@property (weak, nonatomic) IBOutlet UITableView *categoryTbl;
@property (weak, nonatomic) IBOutlet UILabel *totalLbl;
@property (weak, nonatomic) IBOutlet UILabel *canuseLbl;
@property (weak, nonatomic) IBOutlet UILabel *iceLbl;
@property (weak, nonatomic) IBOutlet UILabel *waitreleaseLbl;

@property (weak, nonatomic) IBOutlet UIButton *inCoinBtn;
@property (weak, nonatomic) IBOutlet UIButton *outCoinBtn;

@end

@implementation MyAssetsVC

#pragma mark --method

- (IBAction)inCoiinAction:(UIButton *)sender {
    
    TopUpController *TUVC = [[TopUpController alloc] init];
    TUVC.hidesBottomBarWhenPushed = YES;
    TUVC.isUP = @"1";
    [self.navigationController pushViewController:TUVC animated:YES];
    
}

- (IBAction)outCoinAction:(UIButton *)sender {
    
    TopUpController *TUVC = [[TopUpController alloc] init];
    TUVC.hidesBottomBarWhenPushed = YES;
    TUVC.isUP = @"2";
    [self.navigationController pushViewController:TUVC animated:YES];
    
}

- (void)_initView{
    _outCoinBtn.layer.borderWidth = 1.f;
    _outCoinBtn.layer.borderColor = BaseColor.CGColor;
    _categoryTbl.delegate = self;
    _categoryTbl.dataSource = self;
    _categoryTbl.tableFooterView = [UIView new];
    _categoryTbl.showsVerticalScrollIndicator = false;
    [_categoryTbl registerNib:[UINib nibWithNibName:@"AccountCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"accountCell"];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"我的资产";
    _oldIndex = 0;
    _dataArr = [NSMutableArray array];
    _dataDic = [NSMutableDictionary dictionary];
    [self _initView];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self requestData];
    
}

- (void)requestData{
    
    NSString *url = [NSString stringWithFormat:@"http://119.160.234.147:8080/userOpt/myWallet"];
    [NetRequestTool get:url
                 params:nil
                success:^(id  _Nonnull json) {
                 
                    self->_dataDic = json;
                    if (self->_dataDic != nil) {
                        
                        self->_dataArr = [[self->_dataDic allKeys] mutableCopy];
                        [self->_categoryTbl reloadData];
                        
                    }else{
                        [SVProgressHUD showErrorWithStatus:@"加载失败"];
                    }
                    
                    
                } failure:^(NSError * _Nonnull error) {
                    
                }];
    
}


#pragma mark ---tableview
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  55.f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"accountCell";
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[AccountCell alloc] init];
    }
    
    cell.coinText.text = _dataArr[indexPath.row];
    if (indexPath.row == _oldIndex) {
        cell.rightView.backgroundColor = BaseColor;
        //给页面复制
        NSDictionary *dic = [_dataDic objectForKey:[_dataArr objectAtIndex:indexPath.row]];
        AccountModel *model = [[AccountModel mj_objectArrayWithKeyValuesArray:@[dic]] firstObject];
        _totalLbl.text = model.ze;
        _canuseLbl.text = model.storage;
        _iceLbl.text = model.deposit ;
        _waitreleaseLbl.text = model.dsf;
        
    }else{
        cell.rightView.backgroundColor = [UIColor clearColor];
    }
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    _oldIndex = indexPath.row;
    [tableView reloadData];
    
    
}
@end

